import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;

/**
 * Write a description of class Attachable here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Attachable extends Actor
{
    public int cordX=0;//dummies. Don't make any sudden movements
    public int cordY=0;
    
    public double X;
    public double Y;// some unknown number. not even we know how these work...

    public double Px = 0;
    public double Py = 0;
    
    public static final int HULL_STRUCTURE = 0;
    public static final int PLAYER = 1;
    public static final int TRACTOR = 2;
    public static final int BOMB = 3;
    public static final int ANIMATION = 4;
    public static final int DUMMY = 5;
    public int type = 0;
    
    private int count = 0;
    
    protected boolean isAttached = false;//regulating of attachment points
    
    public Location[] aPts;
    
    protected Location attached;
    
    double renderX, renderY;
    
    public double rotation = 0;
    public double rotationD=0;
    
    protected boolean invisible =false;
    private boolean isInvisible = false;
    private static final GreenfootImage i = new GreenfootImage(1,1);
    private final GreenfootImage returnI;
    
    
    
    public Attachable(int tpe, GreenfootImage look){
        switch(tpe){
            
        case HULL_STRUCTURE : aPts = new Location[4];
        break;
        case PLAYER : aPts = new Location[4];
        break;
        case TRACTOR : aPts  = new Location[1];
         break;
        case BOMB : aPts = new Location[1];
        break;
        case ANIMATION : aPts = new Location[1];aPts[0] = new Location(0,0,0);break;
        case DUMMY : aPts = new Location[1];aPts[0] = new Location(0,0,0);break;
        
        }
    returnI = look;
    type = tpe;
    }
    
    public void setCoords(int xc, int yc){
    X = xc;
    Y = yc;
    }
    
    public void setMomentum(double x, double y){
        Px = x;
        Py = y;
    
    }

    public void attach(Location l){
    
        attached = l;
        attached.isUsed = true;
        isAttached = true;
    }
    public void dettach(){
    attached.isUsed = false;
    attached = null;
    isAttached = false;
    X = getX()-Play.getTrueX();
    Y = getY()-Play.getTrueY();
    //System.out.println(X+" , "+Y);
    }
    public Location getAttached(){
    /*if(!isAttached){
    return null;
    }else{*/
    return attached;
   // }
    }

    
    public Location[] getAttachmentPoints(){
       return aPts;
    }
    public void addAttatchmentPoint(Location l){
    aPts[count] = l;
    count++;
    }
    
    public static Location getNearest(int X, int Y, Attachable a){
      double closestDist= Math.pow(2, 31);
      Location closest = null;
      boolean throwOut = false;
      ArrayList<Location> aloc = getAllAttachmentPoints();
        for(int x = 0 ; x < aloc.size(); x++){
            for( int y = 0 ; y < a.getAttachmentPoints().length; y++){
             if(aloc.get(x) == a.getAttachmentPoints()[y]){
                 throwOut = true;
                 break;
                }
            }
            if(!throwOut){
            
                
                int xdif,ydif;
                xdif = aloc.get(x).getRealX() - X;
                ydif = aloc.get(x).getRealY() - Y;
                
                double xdou,ydou,distance;
                xdou = Math.pow(xdif, 2);
                ydou = Math.pow(ydif, 2);
                distance = Math.sqrt(xdou+ydou);
                if(distance < closestDist){
                    closestDist = distance;
                    closest = aloc.get(x);
                }
                
            }
            throwOut = false;
        }
        return closest;

      
    }
    
    
    public static Attachable getOwner(Location ASeriousVariable){
       for(int x = 0; x < getAllAttachmentPoints().size(); x++){
        for(int y = 0; y <Play.returnEverySingleAttachableInThisWorld()[x].getAttachmentPoints().length;y++){
          if(Play.returnEverySingleAttachableInThisWorld()[x].getAttachmentPoints()[y] == getAllAttachmentPoints().get(x)){
           return Play.returnEverySingleAttachableInThisWorld()[y];
          }
        }
      }
      return null;
    }
    
    public static ArrayList<Location> getAllAttachmentPoints(){
    Attachable[] p = new Attachable[Play.returnEverySingleAttachableInThisWorld().length];
    p = Play.returnEverySingleAttachableInThisWorld();
    ArrayList<Location> loc = new ArrayList<Location>();
    for(int z = 0; z < p.length ; z++){
    for(int x = 0; x <p[z].getAttachmentPoints().length; x++
    ){
        loc.add(p[z].getAttachmentPoints()[x]);
    }}
    System.out.println(loc.toArray().toString());
    return loc;
    }
    
    public void act(){
    
        
        renderX = Play.getTrueX()+ X;
        renderY = Play.getTrueY()+ Y;
        X+= Px;
        Y+= Py;
        if(isAttached && getAttached() != null){
          renderX = getAttached().getRealX();
          renderY = getAttached().getRealY();
          rotation = 0;
          rotationD = 0;
          
        }
        /*if(renderX<0 ||renderX > 1000 || renderY < 0 || renderY > 600){
        invisible = true;
        }else{
            invisible = false;
        }*/
        if(invisible && !isInvisible){
        setImage(i);
        isInvisible = true;
        }else if(!invisible && isInvisible){
        setImage(returnI);
        isInvisible = false;
        }
        if(rotation != 0){
          rotationD += rotation;
          setRotation((int)rotationD);
        }
        setLocation((int)renderX,(int)renderY);

    }
  
}
